//
//  EngLeaderIndexController.m
//  Estate
//
//  Created by heyong on 16/9/6.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "EngLeaderIndexController.h"

@interface EngLeaderIndexController ()<UIScrollViewDelegate>

@end

@implementation EngLeaderIndexController
-(UIScrollView *)contentScrollview{
    if(_contentScrollview==nil){
        _contentScrollview=[[UIScrollView alloc]init];
    }
    return _contentScrollview;
}
//- (UISegmentedControl *)headerSegment{
//    if(_headerSegment==nil){
//        if([_identifier isEqualToString:@"detail"]){
//            NSArray *arr_1 = [[NSArray alloc]initWithObjects:@"待办任务",@"已完成任务",nil];
//            _headerSegment=[[UISegmentedControl alloc]initWithItems:arr_1];
//        }{
//            NSArray *arr_2 = [[NSArray alloc]initWithObjects:@"待办巡检项",@"已完成巡检项",nil];
//            _headerSegment=[[UISegmentedControl alloc]initWithItems:arr_2];
//        }
//        _headerSegment.frame=CGRectMake(10, 74, DEF_SCREEN_WIDTH-20, 35*BIZ);
//    }
//    return _headerSegment;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加滚动视图
    [self.view addSubview:self.contentScrollview];
    //添加选择器
    [self.view addSubview:self.headerSegment];
    [self createView];
}

#pragma mark -创建视图
- (void) createView{
    self.title=@"巡检";
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    
    //设置子视图
    if([_identifier isEqualToString:@"detail"]){
        NSArray *arr_1 = [[NSArray alloc]initWithObjects:@"待办巡检项",@"已完成巡检项",nil];
        self.headerSegment=[[UISegmentedControl alloc]initWithItems:arr_1];
        
        //设置滚动视图
        [self setUpScrollView];
        [self setUpChildViewControll];
        
        //判断是待办 还是已完成
        if(self.finsh){
            //设置默认选中栏
            self.headerSegment.selectedSegmentIndex=1;
            [self segmentSelect:self.headerSegment];

        }else{
            self.headerSegment.selectedSegmentIndex=0;
        }
        

        
    }else{
        
        NSArray *arr_1 = [[NSArray alloc]initWithObjects:@"待办任务",@"已完成任务",nil];
        self.headerSegment=[[UISegmentedControl alloc]initWithItems:arr_1];
        self.headerSegment.selectedSegmentIndex=0;
        //设置滚动视图
        [self setUpScrollView];
        
        [self setUpChildViewControll_Index];
    }
    
    self.headerSegment.frame=CGRectMake(10, 74, DEF_SCREEN_WIDTH-20, 35*BIZ);
    [self.view addSubview:self.headerSegment];
    
    [self.contentScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerSegment.mas_bottom).offset(10);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(667*BIZ);
    }];

    //设置选择器样式
    [_headerSegment setTintColor:[TTTheme defaultManger].defaultColor];
    // 未选中颜色
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[TTTheme defaultManger].selectColor,NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateNormal];
    //选中颜色 字体大小
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:17.*BIZ]} forState:UIControlStateSelected];
 
    //为选择器添加事件
    [_headerSegment addTarget:self action:@selector(segmentSelect:) forControlEvents:UIControlEventValueChanged];
}

//点击选择器事件
-(void)segmentSelect:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    CGRect frame = self.contentScrollview.frame;
    frame.origin.x = index * DEF_SCREEN_WIDTH;
    frame.origin.y = 0;
    frame.size.width=DEF_SCREEN_WIDTH;
    frame.size.height=667*BIZ;
    [self.contentScrollview scrollRectToVisible:frame animated:YES];
}

//设置滚动视图属性
-(void)setUpScrollView{
    
    _contentScrollview.pagingEnabled = YES;
    _contentScrollview.bounces=NO;
    _contentScrollview.delegate = self;
    _contentScrollview.showsHorizontalScrollIndicator = NO;
    //方向锁
    _contentScrollview.directionalLockEnabled = YES;
    //取消自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _contentScrollview.contentSize = CGSizeMake(DEF_SCREEN_WIDTH*2, DEF_SCREEN_HEIGHT);
    
}

/**
 *  设置控制的每一个子控制器
 */

-(void)setUpChildViewControll_Index{
    //初始化视图控制器
   EngIndexController_Gtasks *gtaVC = [[EngIndexController_Gtasks alloc]init];
   EngIndexController_finish *finVC = [[EngIndexController_finish alloc]init];
    
    //指定该控制器为其子控制器
    [self addChildViewController:gtaVC];
    [self addChildViewController:finVC];
 
    //将视图view加入到scrollview上
    [_contentScrollview addSubview:gtaVC.view];
    [_contentScrollview addSubview:finVC.view];

    //!!!设置控制器的位置
    CGRect Rect = gtaVC.view.frame;
    Rect.origin.x = 0;
    Rect.size.height =DEF_SCREEN_HEIGHT-100;
    gtaVC.view.frame = Rect;
    
    CGRect jljlCtrRect = finVC.view.frame;
    jljlCtrRect.origin.x = DEF_SCREEN_WIDTH;
    jljlCtrRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    finVC.view.frame = jljlCtrRect;
}


-(void)setUpChildViewControll{
    //初始化视图控制器
    EngController_gstasks *gtaVC = [[EngController_gstasks alloc]init];
    gtaVC.processId=self.processId;
    EngController_finish *finVC = [[EngController_finish alloc]init];
    finVC.processId=self.processId;
    
    //指定该控制器为其子控制器
    [self addChildViewController:gtaVC];
    [self addChildViewController:finVC];
    
    //将视图view加入到scrollview上
    [_contentScrollview addSubview:gtaVC.view];
    [_contentScrollview addSubview:finVC.view];
    
    //!!!设置控制器的位置
    CGRect Rect = gtaVC.view.frame;
    Rect.origin.x = 0;
    Rect.size.height =DEF_SCREEN_HEIGHT-100;
    gtaVC.view.frame = Rect;
    
    CGRect jljlCtrRect = finVC.view.frame;
    jljlCtrRect.origin.x = DEF_SCREEN_WIDTH;
    jljlCtrRect.size.height = CGRectGetHeight(_contentScrollview.frame);
    finVC.view.frame = jljlCtrRect;

}

#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger ratio = round(offSetX / DEF_SCREEN_WIDTH);
    _headerSegment.selectedSegmentIndex = ratio;
}


@end
